/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tecpizza;

import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import negocio.Pedido;

/**
 *
 * @author tecnico
 */
public class StatusPedido extends javax.swing.JFrame {

    /**
     * Creates new form StatusPedido
     */
    public StatusPedido() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelNumeroPedido = new javax.swing.JLabel();
        jLabelStatus = new javax.swing.JLabel();
        jLabelValor = new javax.swing.JLabel();
        jLabelCliente = new javax.swing.JLabel();
        jTextFieldNumeroPedido = new javax.swing.JTextField();
        jTextFieldStatus = new javax.swing.JTextField();
        jTextFieldValor = new javax.swing.JTextField();
        jButtonEntregar = new javax.swing.JButton();
        jButtonConcluir = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableStatus = new javax.swing.JTable();
        jLabelStatusPedido = new javax.swing.JLabel();
        jButtonLimparTela = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaCliente = new javax.swing.JTextArea();

        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jLabelNumeroPedido.setText("Número Pedido:");

        jLabelStatus.setText("Status:");

        jLabelValor.setText("Valor:");

        jLabelCliente.setText("Cliente:");

        jButtonEntregar.setText("Entregar");
        jButtonEntregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEntregarActionPerformed(evt);
            }
        });

        jButtonConcluir.setText("Concluir");
        jButtonConcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConcluirActionPerformed(evt);
            }
        });

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jTableStatus.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Pedido", "Cliente", "Status", "Valor", "Alterar"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Float.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableStatus.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableStatusMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableStatus);
        jTableStatus.getColumnModel().getColumn(0).setResizable(false);
        jTableStatus.getColumnModel().getColumn(1).setResizable(false);
        jTableStatus.getColumnModel().getColumn(2).setResizable(false);
        jTableStatus.getColumnModel().getColumn(3).setResizable(false);
        jTableStatus.getColumnModel().getColumn(4).setResizable(false);

        jLabelStatusPedido.setFont(new java.awt.Font("Arial Black", 1, 14)); // NOI18N
        jLabelStatusPedido.setText("Status do Pedido");

        jButtonLimparTela.setText("Limpar Tela");
        jButtonLimparTela.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimparTelaActionPerformed(evt);
            }
        });

        jTextAreaCliente.setColumns(20);
        jTextAreaCliente.setRows(5);
        jScrollPane2.setViewportView(jTextAreaCliente);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 563, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 28, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(102, 102, 102)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelNumeroPedido)
                            .addComponent(jLabelStatus)
                            .addComponent(jLabelValor)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButtonEntregar)
                                .addGap(54, 54, 54)
                                .addComponent(jButtonConcluir))
                            .addComponent(jLabelCliente)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(206, 206, 206)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelStatusPedido)
                            .addComponent(jTextFieldStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jButtonCancelar)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTextFieldNumeroPedido, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jButtonLimparTela))
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabelStatusPedido)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldNumeroPedido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonLimparTela)
                    .addComponent(jLabelNumeroPedido))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelStatus))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(jLabelCliente))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelValor))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonEntregar)
                        .addComponent(jButtonConcluir))
                    .addComponent(jButtonCancelar))
                .addGap(31, 31, 31)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTableStatusMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableStatusMouseClicked

        Pedido status = new Pedido();
        if(jTableStatus.getModel().getValueAt(jTableStatus.getSelectedRow(), 4)==true){
            jTextFieldNumeroPedido.setText(jTableStatus.getModel().getValueAt(jTableStatus.getSelectedRow(), 0).toString());
            jTextFieldStatus.setText(jTableStatus.getModel().getValueAt(jTableStatus.getSelectedRow(), 2).toString());
            jTextFieldValor.setText(jTableStatus.getModel().getValueAt(jTableStatus.getSelectedRow(), 3).toString());
            jTextAreaCliente.setText(jTableStatus.getModel().getValueAt(jTableStatus.getSelectedRow(), 1).toString());
            
            jTableStatus.getModel().setValueAt(false, jTableStatus.getSelectedRow() ,4);
        }        
    }//GEN-LAST:event_jTableStatusMouseClicked

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        
        atualizaTabela();
    }//GEN-LAST:event_formWindowActivated

    private void jButtonEntregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEntregarActionPerformed

        Pedido pedido = new Pedido();               
        
        String statusAtual = jTextFieldStatus.getText();
        int numPedidoTemp = Integer.parseInt(jTextFieldNumeroPedido.getText());
        
        
        if(statusAtual.equals("CONCLUIDO")){
            JOptionPane.showMessageDialog(null, "Status não pode ser alterado!");
        }
        else if(statusAtual.equals("CANCELADO")){
            JOptionPane.showMessageDialog(null, "Status não pode ser alterado!");
        }
        else if(statusAtual.equals("ENTREGA")){
            JOptionPane.showMessageDialog(null, "Nenhuma alteração efetuada no status!");
        }
        else if(statusAtual.equals("PREPARO")){
            String novoStatus = "ENTREGA";
            pedido.alterarStatus(novoStatus, numPedidoTemp);
            JOptionPane.showMessageDialog(null, "Status alterado com sucesso!");
            limpaTela();
        }
    }//GEN-LAST:event_jButtonEntregarActionPerformed

    private void jButtonConcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConcluirActionPerformed
        
        Pedido pedido = new Pedido();               
        
        String statusAtual = jTextFieldStatus.getText();
        int numPedidoTemp = Integer.parseInt(jTextFieldNumeroPedido.getText());
        
        
        if(statusAtual.equals("PREPARO")){
            JOptionPane.showMessageDialog(null, "Status ainda não pode ser concluído!");
        }
        else if(statusAtual.equals("CANCELADO")){
            JOptionPane.showMessageDialog(null, "Status não pode ser alterado!");
        }
        else if(statusAtual.equals("CONCLUIDO")){
            JOptionPane.showMessageDialog(null, "Nenhuma alteração efetuada no status!");
        }
        else if(statusAtual.equals("ENTREGA")){
            String novoStatus = "CONCLUIDO";
            pedido.alterarStatus(novoStatus, numPedidoTemp);
            JOptionPane.showMessageDialog(null, "Status alterado com sucesso!");
            limpaTela();
        }
    }//GEN-LAST:event_jButtonConcluirActionPerformed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        
        Pedido pedido = new Pedido();               
        
        String statusAtual = jTextFieldStatus.getText();
        int numPedidoTemp = Integer.parseInt(jTextFieldNumeroPedido.getText());
        
        
        if(statusAtual.equals("CONCLUIDO")){
            JOptionPane.showMessageDialog(null, "Status não pode ser alterado!");
        }
        
        else if(statusAtual.equals("CANCELADO")){
            JOptionPane.showMessageDialog(null, "Nenhuma alteração efetuada no status!");
        }
        else if(statusAtual.equals("PREPARO")){
            String novoStatus = "CANCELADO";
            pedido.alterarStatus(novoStatus, numPedidoTemp);
            JOptionPane.showMessageDialog(null, "Status alterado com sucesso!");
            limpaTela();
        }
        else if(statusAtual.equals("ENTREGA")){
            String novoStatus = "CANCELADO";
            pedido.alterarStatus(novoStatus, numPedidoTemp);
            JOptionPane.showMessageDialog(null, "Status alterado com sucesso!");
            limpaTela();
        }        
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jButtonLimparTelaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimparTelaActionPerformed

        limpaTela();
    }//GEN-LAST:event_jButtonLimparTelaActionPerformed

    private void limpaTela(){
        jTextAreaCliente.setText("");
        jTextFieldNumeroPedido.setText("");
        jTextFieldStatus.setText("");
        jTextFieldValor.setText("");
    }
    
    private void atualizaTabela(){
       
        DefaultTableModel tTabela = (DefaultTableModel) jTableStatus.getModel();
        tTabela.setNumRows(0);
        Pedido ped = new Pedido();
        
        List<Pedido> pedidos = ped.getAll();
        if (pedidos!=null){
            for (int linha = 0; linha < pedidos.size(); linha++)
            {
                Pedido pedido = pedidos.get(linha);

                tTabela.addRow(new Object[]{1});

                String cliente = pedido.buscaCliente();         
                
                jTableStatus.setValueAt(pedido.getCodigo(), linha, 0);
                jTableStatus.setValueAt(cliente, linha, 1);
                jTableStatus.setValueAt(pedido.getStatus(), linha, 2);
                jTableStatus.setValueAt(pedido.getTotal(), linha, 3);
            }
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StatusPedido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StatusPedido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StatusPedido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StatusPedido.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new StatusPedido().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonConcluir;
    private javax.swing.JButton jButtonEntregar;
    private javax.swing.JButton jButtonLimparTela;
    private javax.swing.JLabel jLabelCliente;
    private javax.swing.JLabel jLabelNumeroPedido;
    private javax.swing.JLabel jLabelStatus;
    private javax.swing.JLabel jLabelStatusPedido;
    private javax.swing.JLabel jLabelValor;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTableStatus;
    private javax.swing.JTextArea jTextAreaCliente;
    private javax.swing.JTextField jTextFieldNumeroPedido;
    private javax.swing.JTextField jTextFieldStatus;
    private javax.swing.JTextField jTextFieldValor;
    // End of variables declaration//GEN-END:variables
}